package com.gz.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/createSession")
public class CreateSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.创建session
        //2.一个会话只能有一个session，是唯一的
        HttpSession session = request.getSession();
        session.setAttribute("name","jack");
        session.setMaxInactiveInterval(60);
        //如果调用了invalidate()会直接删除session
        //如果想删除session对象的某个属性，用removeAttribute("xx");
        System.out.println("createSession: "+session.getId());
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println("<h1>创建session成功</h1>");
        writer.flush();
        writer.close();
    }
}
